package com.snopy.leetcode.index1_1000.index901_1000;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Question954
 * @description 二倍数对数组
 * 给定一个长度为偶数的整数数组 arr，只有对 arr 进行重组后可以满足 “对于每个 0 <=i < len(arr) / 2，都有 arr[2 * i + 1] = 2 * arr[2 * i]”时，返回 true；否则，返回 false。
 *
 * 
 *
 * 示例 1：
 *
 * 输入：arr = [3,1,3,6]
 * 输出：false
 * 示例 2：
 *
 * 输入：arr = [2,1,2,6]
 * 输出：false
 * 示例 3：
 *
 * 输入：arr = [4,-2,2,-4]
 * 输出：true
 * 解释：可以用 [-2,-4] 和 [2,4] 这两组组成 [-2,-4,2,4] 或是 [2,4,-2,-4]
 *
 * @email 77912204@qq.com
 * @date 2022/04/01 2:16
 */
public class Question954 {
    public static void main(String[] args) {
        int[] nums = new int[]{3,1,3,6};
        boolean flag = canReorderDoubled(nums);
        System.out.println(flag);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i]+"\t");
        }
    }
    public static boolean  check(int[] arr) {
        for (int i = 0;i<arr.length/2;i++){
            if (arr[2*i+1]!=2*arr[2*i]){
                return false;
            }
        }
        return true;
    }
    public static boolean  canReorderDoubled(int[] arr) {
        if (arr.length<2){
            return false;
        }
        Map<Integer,Integer> container = new HashMap<>(arr.length);
        Arrays.sort(arr);
        for (Integer num:arr) {
            if (num<0){
                if (container.containsKey(2*num)){
                    if (container.get(2*num)>0){
                        container.put(2*num,container.get(2 * num)-1);
                    }else {
                        container.remove(2*num);
                    }
                }else {
                    if(container.containsKey(num)){
                        container.put(num,container.get(num)+1);
                    }else {
                        container.put(num,0);
                    }
                }
            }else if (num==0){
                if (container.containsKey(num)){
                    if (container.get(num)>0){
                        container.put(num,container.get(num)-1);
                    }else {
                        container.remove(num);
                    }
                }else {
                    if(container.containsKey(num)){
                        container.put(num,container.get(num)+1);
                    }else {
                        container.put(num,0);
                    }
                }
            }else {
                if (num%2==0&&container.containsKey(num/2)){
                    if (container.get(num/2)>0){
                        container.put(num/2,container.get(num/2)-1);
                    }else {
                        container.remove(num/2);
                    }
                }else{
                    if (container.containsKey(num)){
                        container.put(num,container.get(num)+1);
                    }else {
                        container.put(num,0);
                    }
                }
            }
        }
        return container.isEmpty();
        /*Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            if (container.containsKey(arr[i])&&container.containsKey(2*arr[i])){
                if(container.get(arr[i])>0){
                    container.put(arr[i],container.get(arr[i])-1);
                }else {
                    container.remove(arr[i]);
                }
                if(container.get(2*arr[i])>0){
                    container.put(2*arr[i],container.get(2*arr[i])-1);
                }else {
                    container.remove(2*arr[i]);
                }
            }
        }
        return container.isEmpty();*/
    }
}
